package com.hilaver.dzmis.service.impl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiPantoneColor;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;

public class BiPantoneColorServiceImpl extends AbstractBaseService implements
		IEntityService {

	@Override
	public String edit(Object obj) throws Exception {
		BiPantoneColor pantone = (BiPantoneColor)obj;
		String operation = null;
		if(pantone.getId() != null){
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(obj);
		super.editSysMbox(obj, operation);
		return "{success: true}";
	}
	
	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(BiPantoneColor.class.getName(), id);
		return super.delete(BiPantoneColor.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		return super.get(BiPantoneColor.class.getName(), id);
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return super.getAllPagination(BiPantoneColor.class.getName(), page, sort, order);
	}
	
	public String getAllPagination(int page, String sort, String order, String[] filters)
			throws Exception {
		// TODO Auto-generated method stub
		return super.getAllPagination(BiPantoneColor.class.getName(), page, sort, order, filters);
	}
	
	public String getRealColor(String pantoneColor) throws Exception{
		BiPantoneColor biPantoneColor = new BiPantoneColor();
		biPantoneColor.setNumber(pantoneColor);
		List<BiPantoneColor> pcList = this.dao.findByExample(biPantoneColor, "id", Constants.ORDER_DESC);
		if(pcList.size() > 0){
			return ((BiPantoneColor)pcList.get(0)).getRealColor();
		}
		return "ffffff";
	}
	
	//For translate pantone from html to database
	public static void main(String[] args) throws Exception{
		String pantoneFileStr = "E:\\99 daafee\\06 Design Zone\\03 src\\DesignZone\\WebContent\\ui\\basic_information\\pantone2.html";

		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(pantoneFileStr)));
		String data = "";
		String fragment = "";
		while((data = br.readLine())!=null){
			if(data.indexOf("<td") != -1){
				fragment += data.trim();
				data = br.readLine();
				fragment += data.trim();
			}
			if(data.indexOf("</td>") != -1){
				String pantoneNumber = "";
				String realColor = "";
				fragment += data.trim();
				if(fragment.indexOf("bgcolor=\"") != -1){
					fragment = fragment.substring(fragment.indexOf("bgcolor=\"") + "bgcolor=\"".length());
					//System.out.println(fragment);
					
					if(fragment.indexOf("#") != -1){
						realColor = fragment.substring(1, 7);
					}else{
						realColor = fragment.substring(0, 6);
					}
					pantoneNumber = fragment.substring(fragment.indexOf("\">") + "\">".length(), fragment.indexOf("</td>"));
					System.err.println(pantoneNumber + " " + realColor);
					BiPantoneColor biPC = new BiPantoneColor();
					biPC.setNumber(pantoneNumber);
					biPC.setRealColor(realColor);
					
					BiPantoneColorServiceImpl biPCService = new BiPantoneColorServiceImpl();
					biPCService.edit(biPC);
				}
				
				fragment = "";
			}
		}


	}

}
